home *** CD-ROM | disk | FTP | other *** search
- // WinIni.lib - Cmm routines to interface with Windows' Profile
- // ver.1 (i.e. *.INI) files from DOS.
- //
- // NOTE: This should not be called from CEnvi for Windows
- //
- //**** GetIniString(): return key value from .ini file
- // SYNTAX: string GetIniString(string FileName,string AppName,string KeyName)
- // WHERE: FileName: name of profile (*.INI) file
- // AppName: Application name ([AppName] in .ini files)
- // KeyName: Key name under [AppName] in the .ini file
- // RETURN: Return the profile string if found; else return NULL if not
- // found or if file cannot be read
- //
- //
- //**** WriteIniString(): write key value to .ini file
- // SYNTAX: bool WriteIniString(string FileName,string AppName,string KeyName,
- // string Value[,bool Replace])
- // WHERE: FileName: name of profile file
- // AppName: Application name ([AppName] in .ini files)
- // KeyName: Key name under [AppName] in the .ini file; if NULL then
- // deletes the entire application section
- // Value: String to set KeyName equal to in the .INI file; if NULL
- // then delete this key
- // Replace: Optional (True if not supplied) to Replace any existing key
- // with this value, else replaces the key with this value
- // RETURN: Return FALSE if failure, else success
- //
- //
- //**** GetIniAppList(): Get array of all Application names in a profile file
- // SYNTAX: string[] GetIniAppList(string FileName)
- // WHERE: FileName: name of profile file
- // RETURN: Returns array of all AppNames in ini file; NULL if none found
- //
- //
- //**** GetIniKeyList(): Get array of all keys in a profile file for Application
- // SYNTAX: string[] GetProfileKeyList(string FileName,string AppName)
- // WHERE: FileName: name of profile file
- // AppName: Application name ([AppName] in .ini files)
- // RETURN: Returns array of all keys under AppName; NULL if none found
- // Each element of array has following two values:
- // .Key KeyName string
- // .Value Value string
- //
- //
-
-
- GetIniString(pFileName,pAppName,pKeyName)
- {
- if ( lList = GetIniKeyList(pFileName,pAppName) ) {
- lKeyCount = 1 + GetArraySpan(lList);
- for ( lIdx = 0; lIdx < lKeyCount; lIdx++ ) {
- if ( !stricmp(pKeyName,lList[lIdx].Key) )
- return lList[lIdx].Value;
- }
- }
- return NULL;
- }
-
- WriteIniString(pFileName,pAppName,pKeyName,pValue,pReplace)
- {
- lAppNameLen = strlen(pAppName);
-
- // read file
- if ( !ReadIniFile(pFileName,lIniList) )
- return False;
-
- // locate the section in the IniFile for pAppName
- lAppMax = GetArraySpan(lIniList);
- for ( lAppIdx = 1; lAppIdx <= lAppMax; lAppIdx++ ) {
- if ( !strnicmp(pAppName,lIniList[lAppIdx].App,lAppNameLen) )
- break;
- }
- if ( lAppIdx <= lAppMax ) {
- if ( !pKeyName ) {
- // pKeyName is NULL, and so delete this section
- undefine( lIniList[lAppIdx].Key );
- } else {
- lKeyNameLen = sprintf(lKeyName,"%s=",pKeyName);
- // application section was found; if replace and found then overwrite
- // else write at the end.
- lKeyMax = GetArraySpan(lKeys = lIniList[lAppIdx].Key);
- lReplaced = (pValue == NULL);
- if ( va_arg() < 5 || pReplace ) {
- for ( lIdx = 1; lIdx <= lKeyMax; lIdx++ ) {
- if ( !strnicmp(lKeyName,lKeys[lIdx],lKeyNameLen) ) {
- if ( !pValue ) {
- // delete this key
- lKeys[lIdx] = NULL;
- } else {
- sprintf(lKeys[lIdx],"%s=%s\n",pKeyName,pValue);
- lReplaced = True;
- }
- }
- }
- }
- if ( !lReplaced ) {
- // add this value to the end of keys
- lLast = lKeys[lKeyMax];
- if ( lLast[0] && lLast[0] != '\n' )
- lLast = lKeys[++lKeyMax];
- sprintf(lLast,"%s=%s\n",pKeyName,pValue);
- }
- }
- } else {
- // application section was not found, so add a new application section
- strcpy(lIniList[lAppMax+1].App,pAppName);
- sprintf(lIniList[lAppMax+1].Key[0],"[%s]\n",pAppName);
- sprintf(lIniList[lAppMax+1].Key[1],"%s=%s\n",pKeyName,pValue);
- }
- return WriteIniFile(pFileName,lIniList);
- }
-
-
- //GetIniString(pFileName,pAppName,pKeyName)
- //{
- // lAppNameLen = strlen(pAppName);
- // lKeyNameLen = sprintf(lKeyName,"%s=",pKeyName);
- // if ( ReadIniFile(pFileName,lIniList) ) {
- // lAppMax = GetArraySpan(lIniList);
- // for ( lAppIdx = 1; lAppIdx <= lAppMax; lAppIdx++ ) {
- // if ( !strnicmp(pAppName,lIniList[lAppIdx].App,lAppNameLen) )
- // break;
- // }
- // if ( lAppIdx <= lAppMax ) {
- // // Application section was found; check each line
- // lKeyMax = GetArraySpan(lKey = lIniList[lAppIdx].Key);
- // for ( lKeyIdx = 1; lKeyIdx <= lKeyMax; lKeyIdx++ ) {
- // if ( !strnicmp(lKeyName,lKey[lKeyIdx],lKeyNameLen) ) {
- // strcpy(lValue,lKey[lKeyIdx]+lKeyNameLen);
- // if ( lValue[strlen(lValue)-1] == '\n' )
- // lValue[strlen(lValue)-1] = '\0';
- // return lValue;
- // }
- // }
- // }
- // }
- // // never found string, so return NULL
- // return NULL;
- //}
-
- GetIniAppList(pFileName)
- {
- if ( !ReadIniFile(pFileName,lIniList) )
- return NULL;
- if ( (lAppMax = GetArraySpan(lIniList)) < 1 )
- return NULL;
- for ( lIdx = 1; lIdx <= lAppMax; lIdx++ )
- strcpy(lList[lIdx-1],lIniList[lIdx].App);
- return lList;
- }
-
- GetIniKeyList(pFileName,pAppName)
- {
- if ( !ReadIniFile(pFileName,lIniList) )
- return NULL;
- if ( (lAppMax = GetArraySpan(lIniList)) < 1 )
- return NULL;
- for ( lIdx = 1; lIdx <= lAppMax; lIdx++ ) {
- if ( !stricmp(pAppName,lIniList[lIdx].App) )
- break;
- }
- if ( lIdx <= lAppMax ) {
- lKeyList = lIniList[lIdx].Key;
- lCount = 0;
- lKeyMax = GetArraySpan(lKeyList);
- for ( lIdx = 1; lIdx <= lKeyMax; lIdx++ ) {
- lString = lKeyList[lIdx];
- if ( lString[0] != ' ' && lString[0] != ';' && (lEqual = strchr(lString,'=')) ) {
- if ( lLen = lEqual - lString ) {
- strncpy(lList[lCount].Key,lString,lLen+1);
- lList[lCount].Key[lLen] = '\0';
- strcpy(lValue,lEqual+1);
- if ( lValue[strlen(lValue)-1] == '\n' )
- lValue[strlen(lValue)-1] = '\0';
- strcpy(lList[lCount].Value,lValue);
- lCount++;
- }
- }
- }
- if ( !lCount )
- return NULL;
- }
- return lList;
- }
-
- ReadIniFile(pFileName,pIniList) // set ini array, return False if cannot
- { // read file
- undefine(pIniList); // undo any previos settings
- if ( !(lFp = fopen(pFileName,"rt")) )
- return False;
- // pretend initial app is blank
- pIniList[lAppIdx = 0].App = "";
- lKeyIdx = -1;
- while ( lLine = fgets(lFp) ) {
- if ( lLine[0] == '[' && strchr(lLine+1,']') ) {
- // this is a new application name
- lEnd = strchr(strcpy(pIniList[++lAppIdx].App,lLine+1),']');
- lEnd[0] = '\0';
- pIniList[lAppIdx].Key[lKeyIdx=0] = lLine;
- } else {
- // this is another line in the application
- pIniList[lAppIdx].Key[++lKeyIdx] = lLine;
- }
- }
- fclose(lFp);
- return True;
- }
-
- WriteIniFile(pFileName,pIniList) // write out ini file; false if error
- {
- if ( !(lFp = fopen(pFileName,"wt")) )
- return False;
- lAppMax = GetArraySpan(pIniList);
- for ( lAppIdx = 0; lAppIdx <= lAppMax; lAppIdx++ ) {
- if ( defined(pIniList[lAppIdx].Key) ) {
- lKeyMax = GetArraySpan(pIniList[lAppIdx].Key);
- for ( lKeyIdx = 0; lKeyIdx <= lKeyMax; lKeyIdx++ ) {
- if ( lLine = pIniList[lAppIdx].Key[lKeyIdx] )
- fwrite(lLine,strlen(lLine),lFp);
- }
- // if the last line wasn't a blank, then write a blank line
- if ( !lLine || (lLine[0] && lLine[0] != '\n') )
- fprintf(lFp,"\n");
- }
- }
- fclose(lFp);
- return True;
- }
-